//package com.jcj.jcjgateway;
//
//import com.jcj.jcjrpc.core.api.LoadBalancer;
//import com.jcj.jcjrpc.core.api.RegistryCenter;
//import com.jcj.jcjrpc.core.cluster.RoundRibonLoadBalancer;
//import com.jcj.jcjrpc.core.meta.InstanceMeta;
//import com.jcj.jcjrpc.core.meta.ServiceMeta;
//import lombok.extern.slf4j.Slf4j;
//import org.jetbrains.annotations.NotNull;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.ResponseEntity;
//import org.springframework.stereotype.Component;
//import org.springframework.web.reactive.function.client.WebClient;
//import org.springframework.web.reactive.function.server.ServerRequest;
//import org.springframework.web.reactive.function.server.ServerResponse;
//import reactor.core.publisher.Mono;
//
//import java.util.List;
//
//@Slf4j
//@Component
//public class GatewayHandler {
//
//    @Autowired
//    RegistryCenter rc;
//
//    LoadBalancer<InstanceMeta> loadBalancer = new RoundRibonLoadBalancer<>();
//
//    Mono<ServerResponse> handle(ServerRequest request) {
//        // 1. 通过请求路径或者服务名
//        String service = request.path().substring(4);
//        ServiceMeta serviceMeta = ServiceMeta.builder().name(service)
//                .app("app1").env("dev").namespace("public").build();
//        // 2. 通过rc拿到所有活着的服务实例
//        List<InstanceMeta> instanceMetas = rc.fetchAll(serviceMeta);
//        // 3. 先简化处理，或者第一个实例url
//
//        InstanceMeta instanceMeta = loadBalancer.choose(instanceMetas);
//        System.out.println(" inst size=" + instanceMetas.size() + ", inst  " + instanceMeta);
//        String url = instanceMeta.toUrl();
//
//        // 4. 拿到请求的报文
//        Mono<String> requestMono = request.bodyToMono(String.class);
//        return requestMono.flatMap(x -> invokeFromRegistry(x, url));
//    }
//
//    private static @NotNull
//    Mono<ServerResponse> invokeFromRegistry(String x, String url) {
//        // 5. 通过webclient发送post请求
//        WebClient client = WebClient.create(url);
//        Mono<ResponseEntity<String>> entity = client.post()
//                .header("Content-Type", "application/json")
//                .bodyValue(x).retrieve().toEntity(String.class);
//        // 6. 通过entity获取响应报文
//        Mono<String> body = entity.map(ResponseEntity::getBody);
//        body.subscribe(souce -> System.out.println("response:" + souce));
//        // 7. 组装响应报文
//        return ServerResponse.ok()
//                .header("Content-Type", "application/json")
//                .header("jcj.gw.version", "v1.0.0")
//                .body(body, String.class);
//    }
//
//
//
//
//}
